struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    explicit ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution
{
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        ListNode* cur1 = l1, *cur2 = l2;
        int tmp = 0;
        ListNode* newhead = new ListNode(0);
        ListNode* prev = newhead;
        while(cur1 || cur2 || tmp)
        {
            if(cur1)
            {
                tmp += cur1->val;
                cur1 = cur1->next;
            }
            if(cur2)
            {
                tmp += cur2->val;
                cur2 = cur2->next;
            }
            ListNode* sum = new ListNode(tmp%10);

            prev->next = sum;
            prev = sum;
            tmp /= 10;
        }
        prev = newhead->next;
        delete newhead;
        return prev;

    }
};